

import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:tencent_rtc/bindings/tx_device_manager_native.dart';
import 'package:tencent_rtc/impl/trtc_cloud_impl.dart';
import 'package:tencent_rtc/trtc_cloud_def.dart';
import 'package:tencent_rtc/tx_device_manager.dart';

import 'package:tencent_rtc/bindings/trtc_cloud_listener_native.dart';

class TXDeviceManagerImpl extends TXDeviceManager {
  final String _tag = "TXDeviceManagerImpl";
  late final TXDeviceManagerNative _deviceManagerNative;
  static TXDeviceManagerImpl? _instance;

  bool _only_support_mobile = Platform.isAndroid || Platform.isIOS;
  bool _only_support_desktop = Platform.isWindows || Platform.isMacOS;
  bool _only_support_windows = Platform.isWindows;

  TXDeviceObserverNative? _deviceObserverNative;

  TXDeviceManagerImpl._internal(dynamic deviceManagerFFIPointer) {
    _deviceManagerNative = TXDeviceManagerNative(deviceManagerFFIPointer);
  }

  factory TXDeviceManagerImpl(dynamic deviceManagerFFIPointer) {
    return _instance ??=
        TXDeviceManagerImpl._internal(deviceManagerFFIPointer);
  }

  static destroyDeviceManager() {
    _instance?._destroy();
    _instance = null;
  }

  _destroy() {
    _deviceObserverNative?.unRegisterNativeListener();
    _deviceObserverNative = null;
  }

  @override
  bool isFrontCamera() {
    if (_only_support_mobile) {
      TRTCCloudImpl.TRTCLog(_tag, "isFrontCamera");
      return _deviceManagerNative.isFrontCamera();
    } else {
      debugPrint("trtc-api not support");
      return false;
    }
  }

  @override
  Future<int> setAudioRoute(TXAudioRoute route) async {
    if (_only_support_mobile) {
      TRTCCloudImpl.TRTCLog(_tag, "setAudioRoute");
      return _deviceManagerNative.setAudioRoute(route.value());
    } else {
      debugPrint("trtc-api not support");
      return -1;
    }
  }

  @override
  Future<int> switchCamera(bool frontCamera) async {
    if (_only_support_mobile) {
      TRTCCloudImpl.TRTCLog(_tag, "switchCamera");
      return _deviceManagerNative.switchCamera(frontCamera);
    } else {
      debugPrint("trtc-api not support");
      return -1;
    }
  }

  @override
  int enableCameraAutoFocus(bool enabled) {
    if (_only_support_mobile) {
      TRTCCloudImpl.TRTCLog(_tag, "enableCameraAutoFocus");
      return _deviceManagerNative.enableCameraAutoFocus(enabled);
    } else {
      debugPrint("trtc-api not support");
      return -1;
    }
  }

  @override
  int enableCameraTorch(bool enabled) {
    if (_only_support_mobile) {
      TRTCCloudImpl.TRTCLog(_tag, "enableCameraTorch");
      return _deviceManagerNative.enableCameraTorch(enabled);
    } else {
      debugPrint("trtc-api not support");
      return -1;
    }
  }

  @override
  int enableFollowingDefaultAudioDevice(TXMediaDeviceType type, bool enable) {
    if (_only_support_desktop) {
      TRTCCloudImpl.TRTCLog(_tag, "enableFollowingDefaultAudioDevice");
      return _deviceManagerNative.enableFollowingDefaultAudioDevice(type, enable);
    } else {
      debugPrint("trtc-api not support");
      return -1;
    }
  }

  @override
  int getApplicationMuteState() {
    if (_only_support_windows) {
      TRTCCloudImpl.TRTCLog(_tag, "getApplicationMuteState");
      return _deviceManagerNative.getApplicationMuteState();
    } else {
      debugPrint("trtc-api not support");
      return -1;
    }
  }

  @override
  int getApplicationPlayVolume() {
    if (_only_support_windows) {
      TRTCCloudImpl.TRTCLog(_tag, "getApplicationPlayVolume");
      return _deviceManagerNative.getApplicationPlayVolume();
    } else {
      debugPrint("trtc-api not support");
      return -1;
    }
  }

  @override
  double getCameraZoomMaxRatio() {
    if (_only_support_mobile) {
      TRTCCloudImpl.TRTCLog(_tag, "getCameraZoomMaxRatio");
      return _deviceManagerNative.getCameraZoomMaxRatio();
    } else {
      debugPrint("trtc-api not support");
      return -1;
    }
  }

  @override
  TXDeviceInfo getCurrentDevice(TXMediaDeviceType type) {
    if (_only_support_desktop) {
      TRTCCloudImpl.TRTCLog(_tag, "getCurrentDevice");
      return _deviceManagerNative.getCurrentDevice(type);
    } else {
      debugPrint("trtc-api not support");
      return TXDeviceInfo();
    }
  }

  @override
  bool getCurrentDeviceMute(TXMediaDeviceType type) {
    if (_only_support_desktop) {
      TRTCCloudImpl.TRTCLog(_tag, "getCurrentDeviceMute");
      return _deviceManagerNative.getCurrentDeviceMute(type);
    } else {
      debugPrint("trtc-api not support");
      return false;
    }
  }

  @override
  int getCurrentDeviceVolume(TXMediaDeviceType type) {
    if (_only_support_desktop) {
      TRTCCloudImpl.TRTCLog(_tag, "getCurrentDeviceVolume");
      return _deviceManagerNative.getCurrentDeviceVolume(type);
    } else {
      debugPrint("trtc-api not support");
      return -1;
    }
  }

  @override
  List<TXDeviceInfo> getDevicesList(TXMediaDeviceType type) {
    if (_only_support_desktop) {
      TRTCCloudImpl.TRTCLog(_tag, "getDevicesList");
      return _deviceManagerNative.getDevicesList(type);
    } else {
      debugPrint("trtc-api not support");
      return [];
    }
  }

  @override
  bool isAutoFocusEnabled() {
    if (_only_support_mobile) {
      TRTCCloudImpl.TRTCLog(_tag, "isAutoFocusEnabled");
      return _deviceManagerNative.isAutoFocusEnabled();
    } else {
      debugPrint("trtc-api not support");
      return false;
    }
  }

  @override
  int setApplicationMuteState(bool mute) {
    if (_only_support_windows) {
      TRTCCloudImpl.TRTCLog(_tag, "setApplicationMuteState");
      return _deviceManagerNative.setApplicationMuteState(mute);
    } else {
      debugPrint("trtc-api not support");
      return -1;
    }
  }

  @override
  int setApplicationPlayVolume(int volume) {
    if (_only_support_windows) {
      TRTCCloudImpl.TRTCLog(_tag, "setApplicationPlayVolume");
      return _deviceManagerNative.setApplicationPlayVolume(volume);
    } else {
      debugPrint("trtc-api not support");
      return -1;
    }
  }

  @override
  void setCameraCaptureParam(TXCameraCaptureParam params) {
    TRTCCloudImpl.TRTCLog(_tag, "setCameraCaptureParam");
    _deviceManagerNative.setCameraCaptureParam(params);
  }

  @override
  int setCameraFocusPosition(double x, double y) {
    if (_only_support_mobile) {
      TRTCCloudImpl.TRTCLog(_tag, "setCameraFocusPosition");
      return _deviceManagerNative.setCameraFocusPosition(x, y);
    } else {
      debugPrint("trtc-api not support");
      return -1;
    }
  }

  @override
  int setCameraZoomRatio(double ratio) {
    if (_only_support_mobile) {
      TRTCCloudImpl.TRTCLog(_tag, "setCameraZoomRatio");
      return _deviceManagerNative.setCameraZoomRatio(ratio);
    } else {
      debugPrint("trtc-api not support");
      return -1;
    }
  }

  @override
  int setCurrentDevice(TXMediaDeviceType type, String deviceId) {
    if (_only_support_desktop) {
      TRTCCloudImpl.TRTCLog(_tag, "setCurrentDevice");
      return _deviceManagerNative.setCurrentDevice(type, deviceId);
    } else {
      debugPrint("trtc-api not support");
      return -1;
    }
  }

  @override
  int setCurrentDeviceMute(TXMediaDeviceType type, bool mute) {
    if (_only_support_desktop) {
      TRTCCloudImpl.TRTCLog(_tag, "setCurrentDeviceMute");
      return _deviceManagerNative.setCurrentDeviceMute(type, mute);
    } else {
      debugPrint("trtc-api not support");
      return -1;
    }
  }

  @override
  int setCurrentDeviceVolume(TXMediaDeviceType type, int volume) {
    if (_only_support_desktop) {
      TRTCCloudImpl.TRTCLog(_tag, "setCurrentDeviceVolume");
      return _deviceManagerNative.setCurrentDeviceVolume(type, volume);
    } else {
      debugPrint("trtc-api not support");
      return -1;
    }
  }

  @override
  int setDeviceObserver(TXDeviceObserver? observer) {
    TRTCCloudImpl.TRTCLog(_tag, "setDeviceObserver");
    if (observer != null) {
      _deviceObserverNative ??= TXDeviceObserverNative(_deviceManagerNative.getNativePointer());
      _deviceObserverNative?.addListener(observer);
    } else {
      _deviceObserverNative?.clearListeners();
    }
    return 0;
  }

  @override
  int startCameraDeviceTest(int viewId) {
    if (_only_support_desktop) {
      TRTCCloudImpl.TRTCLog(_tag, "startCameraDeviceTest");
      return _deviceManagerNative.startCameraDeviceTest(viewId);
    } else {
      debugPrint("trtc-api not support");
      return -1;
    }
  }

  @override
  int startMicDeviceTest(int interval) {
    if (_only_support_desktop) {
      TRTCCloudImpl.TRTCLog(_tag, "startMicDeviceTest");
      return _deviceManagerNative.startMicDeviceTest(interval);
    } else {
      debugPrint("trtc-api not support");
      return -1;
    }
  }

  @override
  int startMicDeviceTestAndPlayback(int interval, bool playback) {
    if (_only_support_desktop) {
      TRTCCloudImpl.TRTCLog(_tag, "startMicDeviceTestAndPlayback");
      return _deviceManagerNative.startMicDeviceTestAndPlayback(interval, playback);
    } else {
      debugPrint("trtc-api not support");
      return -1;
    }
  }

  @override
  int startSpeakerDeviceTest(String filePath) {
    if (_only_support_desktop) {
      TRTCCloudImpl.TRTCLog(_tag, "startSpeakerDeviceTest");
      return _deviceManagerNative.startSpeakerDeviceTest(filePath);
    } else {
      debugPrint("trtc-api not support");
      return -1;
    }
  }

  @override
  void stopCameraDeviceTest() {
    if (_only_support_desktop) {
      TRTCCloudImpl.TRTCLog(_tag, "stopCameraDeviceTest");
      _deviceManagerNative.stopCameraDeviceTest();
    } else {
      debugPrint("trtc-api not support");
      return;
    }
  }

  @override
  void stopMicDeviceTest() {
    if (_only_support_desktop) {
      TRTCCloudImpl.TRTCLog(_tag, "stopMicDeviceTest");
      _deviceManagerNative.stopMicDeviceTest();
    } else {
      debugPrint("trtc-api not support");
      return;
    }
  }

  @override
  void stopSpeakerDeviceTest() {
    if (_only_support_desktop) {
      TRTCCloudImpl.TRTCLog(_tag, "stopSpeakerDeviceTest");
      _deviceManagerNative.stopSpeakerDeviceTest();
    } else {
      debugPrint("trtc-api not support");
      return;
    }
  }


}